from django.db import models
from django.contrib.auth.models import AbstractUser
from datetime import datetime


# Create your models here.

# 复用Django自带的user字段
class UserProfile(AbstractUser):
    GENDER_CHOICES = (
        ("male", "男"), ("female", "女")
    )
    nick_name = models.CharField(max_length=50, verbose_name="昵称", default="")
    birthday = models.DateField(verbose_name="生日", null=True, blank=True)
    gender = models.CharField(verbose_name="性别", choices=GENDER_CHOICES, max_length=6)
    address = models.CharField(max_length=100, verbose_name="地址", default="")
    mobile = models.CharField(max_length=11, verbose_name="手机号码")
    image = models.ImageField(verbose_name="用户头像", upload_to="head_image/%Y/%m", default="head_image/default.png")

    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

    def unread_nums(self):
        # 未读消息数量
        return self.usermessage_set.filter(has_read=False).count()

    def submit_homework(self):
        return self.userhomework_set.values_list('homework_id')

    def home_work_score(self):
        return self.userhomework_set.filter(score__isnull=False).order_by('-add_time')

    def discuss_score(self):
        # return self.coursediscusscomment_set.filter()
        return self.coursediscusscomment_set.all().order_by('-add_time')

    # def is_teacher(self):
    #     flag = self.teacher
    #     if flag:
    #         return True
    #     else:
    #       return  False

    def __str__(self):
        return self.username


# 公用字段设计在最底层不会产生循环引用
class BaseModel(models.Model):
    add_time = models.DateTimeField(verbose_name="添加时间", default=datetime.now)

    class Meta:
        abstract = True
